/*
 *所有关于Bas_Parameter类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Bas_ParameterService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using DGZImp.Core.BaseProvider;
using DGZImp.Core.Extensions.AutofacManager;
using DGZImp.BasProject.DomainModels;
using System.Linq;
using DGZImp.Core.Utilities;
using System.Linq.Expressions;
using DGZImp.Core.Extensions;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using DGZImp.BasProject.IRepositories;
using DGZImp.Core.Exceptions;

namespace DGZImp.BasProject.Services
{
    public partial class Bas_ParameterService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IBas_ParameterRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public Bas_ParameterService(
            IBas_ParameterRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        /// <summary>
        /// 根据module获取参数表中所有参数
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<List<Bas_Parameter>> GetParamerIniByModule(int module)
        {
            if (module < 0)
            {
                throw new Exception("参数模块不能为空");
            }
            var list = await _repository.FindAsync(t => t.ParamModule == module);
            list = list.DistinctBy(t => t.ParamName).OrderBy(t => t.CreateTime).ToList();

            return list;
        }

        /// <summary>
        /// 添加或修改参数
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        /// <exception cref="AppException"></exception>
        public async Task<string> AddOrUpdateParamter(Bas_Parameter data)
        {
            if (data.ParameterId > 0)
            {
                _repository.Update(data, true);
                return "修改成功";
            }
            else
            {
                var flag = await _repository.ExistsAsync(x => x.ParamModule == data.ParamModule && x.ParamName == data.ParamName);
                if (flag) { throw new AppException("已有相同参数类型、参数名的数据存在"); };
                _repository.Add(data, true);
                return "新增成功";
            }
        }

        /// <summary>
        /// 根据模块和参数名获取参数
        /// </summary>
        /// <param name="ParamModule"></param>
        /// <param name="ParamName"></param>
        /// <returns></returns>
        public async Task<List<Bas_Parameter>> GetParametersQuery(int ParamModule, string ParamName)
        {
            var list = await _repository.FindAsync(i => i.ParamModule == ParamModule && i.ParamName == ParamName);
            list = list.OrderBy(i => i.ParamPriority).ToList();
            return list;
        }

        /// <summary>
        /// 批量添加或修改参数
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool RangeAddOrUpdateParams(List<Bas_Parameter> data)

        {
            var addData = data.FindAll(i => i.ParameterId == 0);
            var updateData = data.FindAll(i => i.ParameterId > 0);
            _repository.AddRange(addData, true);
            _repository.UpdateRange(updateData, true);
            return true;

        }
        /// <summary>
        /// 根据id回显
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<Bas_Parameter> GetParameterById(string Id)
        {

            return await _repository.FindFirstAsync(i => i.ParameterId == Convert.ToInt32(Id));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string DeleteParameter(Bas_Parameter model)
        {
            //_repository.Delete(model, true); 此处删除，数据库数据也没有了
            model.IsValid = 0;
            _repository.Update(model, true);
            return "删除成功";
        }


        public async Task<List<Bas_Parameter>> QueryParamIniByNameAndType(string Name, int? Type,
            string Value = null, long? Id = null, string Condition1 = null)
        {
            var lst = await _repository.DbContext.Queryable<Bas_Parameter>()
                .WhereIF(!string.IsNullOrEmpty(Name), t => t.ParamName == Name)
                .WhereIF(Type != null, t => t.ParamModule == Type)
                .WhereIF(!string.IsNullOrEmpty(Value), t => t.ParamValue == Value)
                .WhereIF(Id != null, t => t.ParameterId != Id)
                //.WhereIF(!string.IsNullOrEmpty(condition1), t => t.param_condition1 == condition1)
                .Where(t => t.IsValid == 1)
                .OrderBy(t => t.ParamPriority)
                .ToListAsync();

            return lst;
        }

    }


}
